/*
 * create by Littleken at 2019/3/26 15:07
 */
package com.littleken.product.controller;

import com.littleken.product.VO.ProductInfoVO;
import com.littleken.product.VO.ProductVO;
import com.littleken.product.VO.ResultVO;
import com.littleken.product.dto.CartDTO;
import com.littleken.product.model.ProductCategory;
import com.littleken.product.model.ProductInfo;
import com.littleken.product.service.CategoryService;
import com.littleken.product.service.ProductService;
import com.littleken.product.service.impl.CategoryServiceImpl;
import com.littleken.product.service.impl.ProductServiceImpl;
import com.littleken.product.utils.JsonData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductServiceImpl productService;

    @Autowired
    private CategoryServiceImpl categoryService;

    @GetMapping("/list")
    public ResultVO getProductInfoList(){

        //查询所有上架商品
        //productInfoList 上架商品列表
        List<ProductInfo> productInfoList = productService.findUpAll();

        //查询类目
        //categoryTypeList 查询所有上架商品对应类目
        //productCategoryList 相同类目列表
        List<Integer> categoryTypeList = productInfoList.stream()
                .map(e -> e.getCategoryType())
                .collect(Collectors.toList());
        List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categoryTypeList);

        //数据拼装
        List<ProductVO> productVOList = new ArrayList<>();
        for(ProductCategory productCategory : productCategoryList){
            ProductVO productVO =  new ProductVO();
            productVO.setCategoryType(productCategory.getCategoryType());
            productVO.setCategoryName(productCategory.getCategoryName());

            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for (ProductInfo productInfo : productInfoList){
                if(productInfo .getCategoryType().equals(productCategory.getCategoryType())){
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo,productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }
        return ResultVO.success(productVOList,"成功");
    }

    @PostMapping("/listForOrder")
    public List<ProductInfo> listForOrder(@RequestBody List<String> prductIdList){
        return productService.findList(prductIdList);
    }

    @PostMapping("/decreaseStock")
    public void decreaseStock(@RequestBody List<CartDTO> cartDTOList){
        productService.decreaseStock(cartDTOList);
    }
}
